package backTracking;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

//216. 组合总和 III
public class Num216_combinationSum3 {
    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> combinationSum3(int k, int n) {
        combinationSum3Helper(n , k , 0 , 1);
        return result;

    }
    public void combinationSum3Helper(int targetSum ,int k, int sum , int startIndex) {
        if (path.size() == k){
            if (sum == targetSum)            result.add(new ArrayList<>(path));
            return ;
        }
        for (int i = startIndex; i <= 9 ; i++) {
            sum += i;
            path.add(i);
            combinationSum3Helper(targetSum , k , sum , i + 1);
            sum -= i;
            path.removeLast();
        }

    }
}
